<template>
  <div class="root-elemnt">
    <!-- 搜索区域开始 -->
    <van-nav-bar>
      <van-button slot="title" class="searchBtn" to="search" type="primary" icon="search">
        搜索
      </van-button>
    </van-nav-bar>
    <!-- 搜索区域结束 -->
    <!-- 标签页区域开始 -->
    <van-tabs class="nav"  v-model="active" @change="IsActive">
      <van-tab v-for="item in channelsList" :key="item.id" :title="item.name">
        <Recommend :id="item.id" />
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hanbaobaoMenu" @click="isShowPopups">
        <i slot="title" class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 标签页区域结束 -->
    <van-popup
      v-model="show"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
      <Edit :chanelinfo="channelsList"></Edit>
    </van-popup>
  </div>
</template>

<script>
import { getChannels } from '@/api/homepage/nav.js'
import Recommend from './components/recommend'
import Edit from '../Homepage/components/channel/edit.vue'
import { mapState } from 'vuex'
import { setItem, getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      channelsList: [],
      show: false,
      active: getItem('active') ? getItem('active') : 0
    }
  },
  mounted () {},
  created () {
    this.getChannelsList()
  },
  methods: {
    async getChannelsList () {
      // 判断用户是否登录
      if (this.$store.state.userToken !== null) {
        // 从数据库里取
        const {
          data: {
            data: { channels: res }
          }
        } = await getChannels()
        this.channelsList = res
      } else if (this.sotrageChannels !== null) {
        // 从本地取
        this.channelsList = this.sotrageChannels
      } else {
        const {
          data: {
            data: { channels: res }
          }
        } = await getChannels()
        this.channelsList = res
      }
    },
    IsActive () {
      // 存到本地
      setItem('active', this.active)
    },
    // 显示弹出层
    isShowPopups () {
      this.show = !this.show
    }
  },
  computed: {
    ...mapState(['sotrageChannels'])
  },
  components: { Recommend, Edit }
}
</script>

<style lang="less" scoped>
.root-elemnt {
  /deep/ .van-tabs__content {
    padding-bottom: 100px;
  }
  .van-nav-bar {
    background-color: #3296fa;
    display: flex;
    justify-content: center;
    align-items: center;
    /deep/ .van-nav-bar__title {
      max-width: unset;
    }
    .searchBtn {
      width: 555px;
      height: 64px;
      background: #74b2f0;
      border-radius: 32px;
      color: #ffffff;
      font-size: 28px;
      border: none;
      .van-icon-search {
        color: #ffffff;
        font-size: 32px;
      }
    }
  }
  .nav {
    height: 82px;
    /deep/ .van-tab {
      width: 200px;
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
      font-size: 28px;
      color: #777777;
    }
    .hanbaobaoMenu {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: #fff;
      opacity: 0.902;
      .toutiao-gengduo {
        font-size: 33px;
      }
    }
    .hanbaobaoMenu::before {
      content: "";
      position: absolute;
      left: 0;
      background: url(~@/assets/gradient-gray-line.png);
      background-size: cover;
      width: 1px;
      height: 100%;
    }
    /deep/ .van-tab--active {
      font-size: 30px;
      color: #333;
    }
    /deep/ .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296fa;
      border-radius: 3px;
      position: absolute;
      bottom: 9px;
    }
    /deep/ .van-tabs__nav--line {
      padding-bottom: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
  }
}
</style>
